/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.uct.service.plugins  
 */
package com.apache.uct.service.plugins;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.manager.ProxyManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.database.constant.SystemTools;
import com.apache.database.constant.Validator;
import com.apache.rpc.util.LoadRpcService;
import com.apache.uct.common.LoginUser;

/**
 * description:  根据用户token获取用户对象
 * @author dwusoft 创建时间：2015-3-30  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
public class SsoUserInfoPlugin implements PluginConnector {

	private Logger log = Logger.getLogger(SsoUserInfoPlugin.class);
	private ProxyManager uctProxyManager;

	/**
	 * TODO 根据用户token获取用户对象.  <br>
	 * 参数说明:<br>
	 * token:token串;<br>
	 * userEname:用户账号;
	 */
	public Object execute(ParamsVo vo) throws Exception {
		String userEname = String.valueOf(vo.getParams("userEname"));
		String token = Validator.getDefaultStr(String.valueOf(vo.getParams("token")), "");
		ResultEntity result = new ResultEntity();
		result.setResult("true");
		try {
			if (Validator.isNull(userEname)) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("tokenId", token);
				String sysEname = SystemTools.getInstance().getValue("sysEname");
				map.put("sysEname", sysEname);
				log.info("map=" + map);
				ResultEntity entity = LoadRpcService.service().doService("ssoService", "checkToken", "xml", map, null);
				log.info("entity=" + entity);
				String tokenId = String.valueOf(entity.getEntity());
				if (Validator.isNotNull(tokenId) && token.equals(tokenId)) {
					userEname = Validator.getDefaultStr(userEname, entity.getMessage());
				} else {
					result.setResult("false");
					result.setMessage(entity.getMessage());
					return result;
				}
			}
			ParamsVo pvo = new ParamsVo();
			pvo.setParams("userEname", userEname);
			pvo.setKey("loginUser");
			LoginUser loginUser = (LoginUser) uctProxyManager.doInvoke(pvo);
			result.setEntity(loginUser);
			result.setMessage("获取用户信息成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.setResult("false");
			result.setMessage("发生未知异常！");
		}
		return result;
	}

	public void setUctProxyManager(ProxyManager uctProxyManager) {
		this.uctProxyManager = uctProxyManager;
	}

}
